package cn.cqone.crm.controller;

import cn.cqone.crm.domain.CustomerDevPlan;
import cn.cqone.crm.domain.Employee;
import cn.cqone.crm.domain.PotentialCustomer;
import cn.cqone.crm.domain.Tenant;
import cn.cqone.crm.query.CustomerDevPlanQuery;
import cn.cqone.crm.service.CustomerDevPlanService;
import cn.cqone.crm.service.EmployeeService;
import cn.cqone.crm.service.PotentialCustomerService;
import cn.cqone.crm.utils.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@ResponseBody
@RequestMapping("/potCusDevPlan")
@CrossOrigin
@RequiresPermissions("customer_dev_plan:main")
public class CustomerDevPlanController extends BaseController<CustomerDevPlan,Long,CustomerDevPlanQuery> {

    @Autowired
    private CustomerDevPlanService customerDevPlanService;

    @Autowired
    private PotentialCustomerService potentialCustomerService;

    @Autowired
    private EmployeeService employeeService;

    //联表新增
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public JSONResult insert(@RequestBody CustomerDevPlan customerDevPlan){
        try{
            Long id = customerDevPlan.getInputUser_id().getId();
            Employee e = employeeService.findById(id);
            Tenant tenant = e.getTenant();
            customerDevPlan.setTenant_id(tenant);
            customerDevPlanService.add(customerDevPlan);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("添加失败 请仔细检查");
        }
    }

    //联表修改
    @RequestMapping(value = "/updateAll/{potentialCustomer_id}",method = RequestMethod.POST)
    public JSONResult updateAll(@RequestBody CustomerDevPlan customerDevPlan,
                             @PathVariable("potentialCustomer_id") Long potentialCustomer_id){
        try{
            PotentialCustomer p = potentialCustomerService.findById(potentialCustomer_id);
            customerDevPlan.setPotentialCustomer_id(p);
            customerDevPlanService.update(customerDevPlan);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("修改失败！");
        }
    }
}
